package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmComm;
import com.scmt.hmzd.service.IHmCommService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmCommMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmCommServiceImpl extends ServiceImpl<HmCommMapper, HmComm> implements IHmCommService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmCommMapper hmCommMapper;

    @Override
    public IPage<HmComm> queryHmCommListByPage(HmComm hmComm, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmComm> pageData = new Page<>(page, limit);
        QueryWrapper<HmComm> queryWrapper = new QueryWrapper<>();
        if (hmComm != null) {
            queryWrapper = LikeAllField(hmComm, searchVo);
        }
        IPage<HmComm> result = hmCommMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmComm hmComm, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmComm> queryWrapper = new QueryWrapper<>();
        if (hmComm != null) {
            queryWrapper = LikeAllField(hmComm, null);
        }
        List<HmComm> list = hmCommMapper.selectList(queryWrapper);
        for (HmComm re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("评论用户ID", re.getUserId());
            map.put("用户头像图片", re.getUserImg());
            map.put("评论内容", re.getContent());
            map.put("技术评分", re.getTechnicalScore());
            map.put("服务评分", re.getServiceScore());
            map.put("是否匿名评论 1是0否", re.getAnonymous());
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmComm 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmComm> LikeAllField(HmComm hmComm, SearchVo searchVo) {
        QueryWrapper<HmComm> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(hmComm.getTechnicianId())) {
            queryWrapper.and(i -> i.eq("hm_comm.technician_id", hmComm.getTechnicianId()));
        }
        if (StringUtils.isNotBlank(hmComm.getOrderId())) {
            queryWrapper.and(i -> i.eq("hm_comm.order_id", hmComm.getOrderId()));
        }
        if (StringUtils.isNotBlank(hmComm.getUserId())) {
            queryWrapper.and(i -> i.eq("hm_comm.user_id", hmComm.getUserId()));
        }

        if (StringUtils.isNotBlank(hmComm.getContent())) {
            queryWrapper.and(i -> i.like("hm_comm.content", hmComm.getContent()));
        }
        if (StringUtils.isNotBlank(hmComm.getReplyContent())) {
            queryWrapper.and(i -> i.like("hm_comm.reply_content", hmComm.getReplyContent()));
        }
        if (hmComm.getReplyTime() != null) {
            queryWrapper.and(i -> i.like("hm_comm.reply_time", hmComm.getReplyTime()));
        }
        if (hmComm.getReplySts() != null) {
            queryWrapper.and(i -> i.like("hm_comm.reply_sts", hmComm.getReplySts()));
        }
        if (hmComm.getStatus() != null) {
            queryWrapper.and(i -> i.like("hm_comm.status", hmComm.getStatus()));
        }
        if (hmComm.getEvaluate() != null) {
            queryWrapper.and(i -> i.like("hm_comm.evaluate", hmComm.getEvaluate()));
        }
        if (hmComm.getTechnicalScore() != null) {
            queryWrapper.and(i -> i.like("hm_comm.technical_score", hmComm.getTechnicalScore()));
        }
        if (hmComm.getServiceScore() != null) {
            queryWrapper.and(i -> i.like("hm_comm.service_score", hmComm.getServiceScore()));
        }
        if (hmComm.getAnonymous() != null) {
            queryWrapper.and(i -> i.eq("hm_comm.anonymous", hmComm.getAnonymous()));
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmComm::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        return queryWrapper;
    }
}
